package cn.cdeden.system.mapper;

import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.toolkit.Constants;
import cn.cdeden.common.mybatis.annotation.DataColumn;
import cn.cdeden.common.mybatis.annotation.DataPermission;
import cn.cdeden.common.mybatis.core.mapper.BaseMapperPlus;
import cn.cdeden.system.domain.SysDept;
import cn.cdeden.system.domain.vo.SysDeptVo;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;

import java.util.List;
import java.util.Set;

/**
 * 部门管理 数据层
 *
 * @author Lion Li
 */
public interface SysDeptMapper extends BaseMapperPlus<SysDept, SysDeptVo> {

    /**
     * 查询部门管理数据
     *
     * @param queryWrapper 查询条件
     * @return 部门信息集合
     */
    @DataPermission({
        @DataColumn(key = "deptName", value = "dept_id")
    })
    List<SysDeptVo> selectDeptList(@Param(Constants.WRAPPER) Wrapper<SysDept> queryWrapper);

    @DataPermission({
        @DataColumn(key = "deptName", value = "dept_id")
    })
    long countDeptById(Long deptId);

    /**
     * 根据角色ID查询部门树信息
     *
     * @param roleId            角色ID
     * @param deptCheckStrictly 部门树选择项是否关联显示
     * @return 选中部门列表
     */
    List<Long> selectDeptListByRoleId(@Param("roleId") Long roleId, @Param("deptCheckStrictly") boolean deptCheckStrictly);

    /**
     * 根据部门id查询部门以及父级部门的信息
     */
    List<SysDeptVo> getSuperiorAll(@Param("deptId") Long deptId);

    @Select("SELECT *,dept_id AS id, ischild FROM ( SELECT t1.*, IF ( find_in_set(parent_id, @pids) > 0, @pids := concat(@pids, ',', dept_id), 0 ) AS ischild FROM ( SELECT * FROM sys_dept t ORDER BY parent_id, dept_id ) t1, (SELECT @pids := #{id}) t2 ) t3 WHERE ischild != 0 OR dept_id = #{id}")
    List<SysDeptVo> queryLowerLevel(Long id);

    List<SysDeptVo> querySuperior(String id);

    @Select("SELECT dept.`dept_id` AS id, dept.`parent_id`, dept.`dept_name`, dept.`status`, dept.`create_by`, dept.`update_by`, dept.`create_time`, dept.`update_time` FROM sys_dept dept LEFT JOIN sys_user user ON dept.dept_id = `user`.dept_id WHERE user_id = #{id}")
    SysDeptVo selectDeptByUserId(Long id);
}
